<?php
require_once 'PHPUnit/Framework.php';
require_once 'TVB/PWORKS/util/CommonUtil.class.php';
require_once 'TVB/pms/pdes/common/bootstrap.php';
require_once 'TVB/pms/pdes/model/change_log.php';
require_once 'TVB/pms/pdes/common/util/NewProofreadSend.php';


class ChangeLogTest extends PHPUnit_Framework_TestCase
{
	protected function setUp()
	{
	}

	protected function tearDown()
	{
	}

	public function test()
	{
		//$this->programmeDelete();
		//$this->editProgrammeCase1();
		//$this->editProgrammeCast();
		//$this->editProgrammeImage();
		//$this->editProgrammeVideo();
		//$this->episode();
		//$this->deleteAll();
		$this->getChangeLogByEpisodeId();
	}

	public function serviceFolderItemImage()
	{
	}

	public function programmeDelete()
	{
		$change_log_list = array(
			array(
				'type' => 'chapter-tvbcom_category',
				'keys' => array(
					'chapter_id' => 47,
					'category_id' => 22 
				),
				'is_active' => 0,
				'is_publish' => 0,
				'programme_id' => 88,
				'episode_id',
			),
			array(
				'type' => 'episode-video',
				'keys' => array(
					'episode_id' => 1,
					'video_id' => 1 
				),
				'is_active' => 0,
				'is_publish' => 0,
				'programme_id' => 88,
				'episode_id',
			),
			array(
				'type' => 'programme-tvbcom_category',
				'keys' => array(
					'programme_id' => 88,
					'category_id' => 37
				),
				'is_active' => 0,
				'is_publish' => 0,
				'programme_id' => 88,
				'episode_id',
			),
			array(
				'type' => 'programme_cpl',
				'keys' => array(
					'programme_id' => 88,
					'effective_date' => '2010-04-09 00:00:00' 
				),
				'is_active' => 0,
				'is_publish' => 0,
				'programme_id' => 88,
				'episode_id',
			),

			array(
				'type' => 'folder',
				'keys' => array('id' => 1799),
				'is_active' => 0,
				'is_publish' => 0,
				'programme_id' => 88,
				'episode_id',
			),
			array(
				'type' => 'folder-language',
				'keys' => array(
					'folder_id' => 1799,
					'language_id' => 1
				),
				'is_active' => 0,
				'is_publish' => 0,
				'programme_id' => 88,
				'episode_id',
			),
			array(
				'type' => 'folder-language',
				'keys' => array(
					'folder_id' => 1799,
					'language_id' => 2 
				),
				'is_active' => 0,
				'is_publish' => 0,
				'programme_id' => 88,
				'episode_id',
			),
			array(
				'type' => 'image_folder-config',
				'keys' => array('folder_id' => 1799),
				'is_active' => 0,
				'is_publish' => 0,
				'programme_id' => 88,
				'episode_id',
			),
			array(
				'type' => 'folder_item',
				'keys' => array(
					'folder_id' => 1799,
					'item_id' => 172
				),
				'is_active' => 0,
				'is_publish' => 0,
				'programme_id' => 88,
				'episode_id',
			),
			array(
				'type' => 'folder_item-language',
				'keys' => array(
					'folder_id' => 1799,
					'item_id' => 172,
					'language_id' => 1
				),
				'is_active' => 0,
				'is_publish' => 0,
				'programme_id' => 88,
				'episode_id',
			),
			array(
				'type' => 'folder_item-language',
				'keys' => array(
					'folder_id' => 1799,
					'item_id' => 172,
					'language_id' => 2 
				),
				'is_active' => 0,
				'is_publish' => 0,
				'programme_id' => 88,
				'episode_id',
			),
		);
	
		$ret = NewProofreadSend::getDeleteSQL($change_log_list, FALSE);
		var_dump($ret);
	}

	/*
	 * by donald
	 */
	public function editProgrammeCase1()
	{
		$change_log_list = array(
			array(
				'type' => 'programme',
				'keys' => array('id' => 88),
				'is_active' => 1,
				'is_publish' => 0,
				'programme_id' => 88,
				'episode_id',
			),
			array(
				'type' => 'programme-language',
				'keys' => array(
					'programme_id' => 88,
					'language_id' => 1,
				),
				'is_active' => 1,
				'is_publish' => 0,
				'programme_id' => 88,
				'episode_id',
			),
			array(
				'type' => 'programme-language',
				'keys' => array(
					'programme_id' => 88,
					'language_id' => 2,
				),
				'is_active' => 1,
				'is_publish' => 0,
				'programme_id' => 88,
				'episode_id',
			),
			array(
				'type' => 'programme-tvbcom_category',
				'keys' => array(
					'programme_id' => 88,
					'category_id' => 37
				),
				'is_active' => 1,
				'is_publish' => 0,
				'programme_id' => 88,
				'episode_id',
			),
			array(
				'type' => 'programme_cpl',
				'keys' => array(
					'programme_id' => 88,
					'effective_date' => '2010-04-09 00:00:00' 
				),
				'is_active' => 1,
				'is_publish' => 0,
				'programme_id' => 88,
				'episode_id',
			),
			array(
				'type' => 'programme_premium',
				'keys' => array(
					'programme_id' => 88,
				),
				'is_active' => 1,
				'is_publish' => 0,
				'programme_id' => 88,
				'episode_id',
			),
			array(
				'type' => 'programme_ppv',
				'keys' => array(
					'programme_id' => 88,
				),
				'is_active' => 1,
				'is_publish' => 0,
				'programme_id' => 88,
				'episode_id',
			),
			array(
				'type' => 'programme_subscription',
				'keys' => array(
					'programme_id' => 88,
				),
				'is_active' => 1,
				'is_publish' => 0,
				'programme_id' => 88,
				'episode_id',
			),

			array(
				'type' => 'tag',
				'keys' => array('id' => 4547),
				'is_active' => 1,
				'is_publish' => 0,
				'programme_id' => 88,
				'episode_id',
			),
			array(
				'type' => 'folder',
				'keys' => array('id' => 1799),
				'is_active' => 1,
				'is_publish' => 0,
				'programme_id' => 88,
				'episode_id',
			),
			array(
				'type' => 'image_folder-config',
				'keys' => array('folder_id' => 1799),
				'is_active' => 1,
				'is_publish' => 0,
				'programme_id' => 88,
				'episode_id',
			),
			array(
				'type' => 'folder_item',
				'keys' => array(
					'folder_id' => 1799,
					'item_id' => 172
				),
				'is_active' => 1,
				'is_publish' => 0,
				'programme_id' => 88,
				'episode_id',
			),
			array(
				'type' => 'image',
				'keys' => array('id' => 172),
				'is_active' => 1,
				'is_publish' => 0,
				'programme_id' => 88,
				'episode_id',
			),
			array(
				'type' => 'image',
				'keys' => array('id' => 190),
				'is_active' => 1,
				'is_publish' => 0,
				'programme_id' => 88,
				'episode_id',
			),
			
			//premium video
			array(
				'type' => 'video',
				'keys' => array('id' => 429),
				'is_active' => 1,
				'is_publish' => 0,
				'programme_id' => 88,
				'episode_id',
			),
			//non-premium video
			array(
				'type' => 'video',
				'keys' => array('id' => 431),
				'is_active' => 1,
				'is_publish' => 0,
				'programme_id' => 88,
				'episode_id',
			),
			array(
				'type' => 'cast',
				'keys' => array('id' => 491),
				'is_active' => 1,
				'is_publish' => 0,
				'programme_id' => 88,
				'episode_id',
			),
			/*
			array(
				'type' => 'cast_dictionary',
				'keys' => array('id' => 1),
				'is_active' => 1,
				'is_publish' => 0,
				'programme_id' => 88,
				'episode_id',
			),
			 */

		);

		foreach ($change_log_list as $change_log_detail) {
			change_log::updateOne($change_log_detail);
		}

		$this->publishProgramme();

	}

	/*
	 * by donald
	 */
	public function editProgrammeCast()
	{
		$change_log_list = array(
			array(
				'type' => 'cast',
				'keys' => array('id' => 1438),
				'is_active' => 1,
				'is_publish' => 0,
				'programme_id' => 88,
				'episode_id',
			),
		);

		foreach ($change_log_list as $change_log_detail) {
			change_log::updateOne($change_log_detail);
		}

		$this->publishProgramme();
	}

	public function editProgrammeImage()
	{
		$change_log_list = array(
			array(
				'type' => 'image',
				'keys' => array('id' => 312),
				'is_active' => 1,
				'is_publish' => 0,
				'programme_id' => 88,
				'episode_id',
			),
		);

		foreach ($change_log_list as $change_log_detail) {
			change_log::updateOne($change_log_detail);
		}

		$this->publishProgramme();
	}

	public function editProgrammeVideo()
	{
		$change_log_list = array(
			array(
				'type' => 'video',
				'keys' => array('id' => 457),
				'is_active' => 1,
				'is_publish' => 0,
				'programme_id' => 88,
				'episode_id',
			),
		);

		foreach ($change_log_list as $change_log_detail) {
			change_log::updateOne($change_log_detail);
		}

		$this->publishProgramme();
	}

	public function publishProgramme()
	{
		$programme_id = 88;
		$publish_type = '11';
		$proof = new NewProofreadSend();
		$log_id = CommonUtils::save_log('programme', $programme_id, AC::LOG_OP_PUBLISHING, json_encode(array()));
		$proof_flag = $proof->getProofread(
			Model::$dbo['r']->getDbHandle(), 
			'programme', 
			$programme_id, $log_id, true,  $publish_type);
		$this->assertEquals($proof_flag, TRUE); 	
	}

	public function episode()
	{
		$change_log_list = array(
			array(
				'type' => 'episode',
				'keys' => array('id' => 250),
				'is_active' => 1,
				'is_publish' => 0,
				'programme_id' => 88,
				'episode_id' => 250,
			),
			array(
				'type' => 'episode-language',
				'keys' => array(
					'episode_id' => 250,
					'language_id' => 1
				),
				'is_active' => 1,
				'is_publish' => 0,
				'programme_id' => 88,
				'episode_id' => 250,
			),
			array(
				'type' => 'episode-language',
				'keys' => array(
					'episode_id' => 250,
					'language_id' => 2 
				),
				'is_active' => 1,
				'is_publish' => 0,
				'programme_id' => 88,
				'episode_id' => 250,
			),
			array(
				'type' => 'info_address',
				'keys' => array('id' => 273),
				'is_active' => 1,
				'is_publish' => 0,
				'programme_id' => 88,
				'episode_id' => 250,
			),
			array(
				'type' => 'info_award',
				'keys' => array('id' => 206),
				'is_active' => 1,
				'is_publish' => 0,
				'programme_id' => 88,
				'episode_id' => 250,
			),
			array(
				'type' => 'info_biography',
				'keys' => array('id' => 99),
				'is_active' => 1,
				'is_publish' => 0,
				'programme_id' => 88,
				'episode_id' => 250,
			),
			array(
				'type' => 'info_general_information',
				'keys' => array('id' => 124),
				'is_active' => 1,
				'is_publish' => 0,
				'programme_id' => 88,
				'episode_id' => 250,
			),
			array(
				'type' => 'info_product',
				'keys' => array('id' => 121),
				'is_active' => 1,
				'is_publish' => 0,
				'programme_id' => 88,
				'episode_id' => 250,
			),
			array(
				'type' => 'info_q_a',
				'keys' => array('id' => 115),
				'is_active' => 1,
				'is_publish' => 0,
				'programme_id' => 88,
				'episode_id' => 250,
			),
			array(
				'type' => 'info_recipe',
				'keys' => array('id' => 117),
				'is_active' => 1,
				'is_publish' => 0,
				'programme_id' => 88,
				'episode_id' => 250,
			),
			array(
				'type' => 'info_song',
				'keys' => array('id' => 120),
				'is_active' => 1,
				'is_publish' => 0,
				'programme_id' => 88,
				'episode_id' => 250,
			),
			array(
				'type' => 'image',
				'keys' => array('id' => 160),
				'is_active' => 1,
				'is_publish' => 0,
				'programme_id' => 88,
				'episode_id' => 250,
			),
			array(
				'type' => 'cast',
				'keys' => array('id' => 492),
				'is_active' => 1,
				'is_publish' => 0,
				'programme_id' => 88,
				'episode_id' => 250,
			),
			array(
				'type' => 'folder',
				'keys' => array('id' => 2979),
				'is_active' => 1,
				'is_publish' => 0,
				'programme_id' => 88,
				'episode_id' => 250,
			),
			array(
				'type' => 'folder_item',
				'keys' => array(
					'folder_id' => 2979,
					'item_id' => 160
				),
				'is_active' => 1,
				'is_publish' => 0,
				'programme_id' => 88,
				'episode_id' => 250,
			),


		);
		foreach ($change_log_list as $change_log_detail) {
			change_log::updateOne($change_log_detail);
		}

		$this->publishEpisode();
	}

	public function publishEpisode()
	{
		$episode_id = 250;
		$publish_type = '11';
		$proof = new NewProofreadSend();
		$log_id = CommonUtils::save_log('episode', $episode_id, AC::LOG_OP_PUBLISHING, json_encode(array()));
		$proof_flag = $proof->getProofread(
			Model::$dbo['r']->getDbHandle(), 
			'episode', 
			$episode_id, $log_id, true,  $publish_type);
		$this->assertEquals($proof_flag, TRUE); 	
	}

	public function getChangeLogByEpisodeId()
	{
		$id = '250';
		$change_log_list = change_log::get(
			array(
				'is_publish' => 0,
			   	'episode_id' => $id 
			)
		);

		print_r($change_log_list);
	}

	public function deleteAll()
	{
		change_log::remove(array());
	}
}

?>
